<?php
namespace app\system\controller;

use app\common\model\Param;
use app\common\traits\controller\JsonTips;
use app\system\model\GroupLinkModel;
use app\system\model\LinkModel;
use think\Controller;
use app\common\controller\Base;



class Common extends Base
{
    public $p;
    protected $typeList = ['普通用户', '分销商', '店长', '店员'];

    use JsonTips;

    /**
     * @return \think\response\Json|void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $Navigation = get_navigation();
        $this->assign('_page_navi', $Navigation);
        // dump(request()->action());die;
        if (strtolower(request()->action()) != 'autocal') {
            //是否登录
            if (!session('?manageInfo') || session('manageInfo.id') <= 0) {
                if (request()->isAjax()) {
                    return $this->jsonError('请重新登录');
                } else {
                    return $this->redirect('Login/index');
                }
            }
            $manageInfo = session('manageInfo');
            $this->admin=$manageInfo;
            //判断是否超时
            $paramModel = new Param();
            $paramLocks = $paramModel->valueParam('content', ['id' => 1]);
            $paramLocks = unserialize($paramLocks);
            $lastTime = session('?lastTime') ? session('lastTime') : time();
            if (time() - $lastTime > $paramLocks['locks'] * 60) {
                if (request()->isAjax()) {
                    return $this->jsonError('登录超时，请重新登录');
                } else {
                    return $this->redirect('Login/locks'); //登录已超时
                }
            }
            $this->assign('website_title', isset($paramLocks['website_title']) ? $paramLocks['website_title'] : '');

            !request()->isAjax() && session('lastTime', time()); //存入操作时间

            $linkModel = new LinkModel();
            $linkId = $linkModel->valueLink('id', ['controller' => request()->controller(),'is_delete'=>0]);
            $where = [
                'group_id' => $manageInfo['group_id'],
                'link_id' => $linkId
            ];
            $groupLinkModel = new GroupLinkModel();
            $group_link = $groupLinkModel->findGroupLink($where);
            //判断是否有权限
            if (!$group_link && (request()->controller() != 'Index') && (request()->controller() != 'Common') && (request()->controller() != 'Empty')) {
                if (request()->isAjax()) {
                    return $this->jsonError('非法操作！');
                } else {
                    return $this->error('非法来源！', url('Login/index'));
                }
            }
        }
    }

}
